package com.xbk.seckill.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.sleuth.CurrentTraceContext;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.web.WebFluxSleuthOperators;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author xiexu
 * @date 2025/5/11 18:30
 */
@Slf4j
@Component
public class SeckillGatewayFilter implements GlobalFilter, Ordered {

    @Autowired
    private Tracer tracer;

    @Autowired
    private CurrentTraceContext currentTraceContext;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        WebFluxSleuthOperators.withSpanInScope(
                tracer,
                currentTraceContext,
                exchange,
                () -> log.info("SeckillGatewayFilter|链路追踪")
        );
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
